package com.house.framework.utils;

import com.google.common.collect.Lists;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 分词工具类
 */
public class IKAnalyzerUtil {
    /**
     * 分词方法
     * @param string
     * 传入中文字符串
     * @return list
     * 返回分词后的中文数组
     */
    public static List<String> iKSegmenterToList(String string){
        List<String> list=new ArrayList<>();
        try{
            if(isEmpty(string)){
                return Lists.newArrayList();
            }
            StringReader sr=new StringReader(string);
            IKSegmenter ik=new IKSegmenter(sr, true);
            Lexeme lex;
            while((lex=ik.next())!=null){
                String lexemeText=lex.getLexemeText();
                list.add(lexemeText);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().isEmpty());
    }
}
